﻿using System;
using System.Collections.Generic;
using System.Web.Script.Services;
using System.Web.Services;
using WSU.PMP.BE.DATA.Concrete;
using WSU.PMP.BE.DATA.EF;

namespace WSU.PMP.BE.WEB_SERVICE
{
    /// <summary>
    /// Summary description for PMPWebService
    /// </summary>
    [WebService(Namespace = "http://www.pmp.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class PMPWebService : System.Web.Services.WebService//, IItem, IRecipe, IShoppingList, ICalendar
    {
        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public EfItem getItemByBarcode(string key, string barcode)
        {
            if (key == "pmpkey")
            {
                Item item = new Item();

                return item.getItemByBarcode(barcode);
            }
            return null;
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public EfItem getItemByName(string key, string name)
        {
            if (key == "pmpkey")
            {
                Item item = new Item();

                return item.getItemByName(name);
            }
            return null;
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public List<EfItem> getItemsByName(string key, string name)
        {
            if (key == "pmpkey")
            {
                Item item = new Item();

                return item.getItemsByName(name);
            }
            return null;
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public List<EfItem> getItemsByType(string key, string type)
        {
            if (key == "pmpkey")
            {
                Item item = new Item();

                return item.getItemsByType(type);
            }
            return null;
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public List<EfItem> getItemByCost(string key, string min, string max)
        {
            if (key == "pmpkey")
            {
                Item item = new Item();

                return item.getItemsByCost(min, max);
            }
            return null;
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public void addItem(string key, string Barcode, string Type, string Name, string Description, string Cost, string Notes, string Quantity)
        {
            if (key == "pmpkey")
            {
                Item item = new Item();

                item.addItem(mapToObject(Barcode, Type, Name, Description, Cost, Notes, Quantity));
            }
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public void editItem(string key, string Barcode, string Type, string Name, string Description, string Cost, string Notes, string Quantity)
        {
            if (key == "pmpkey")
            {
                Item item = new Item();

                item.editItem(mapToObject(Barcode, Type, Name, Description, Cost, Notes, Quantity));
            }
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public void deleteItem(string key, string Barcode, string Type, string Name, string Description, string Cost, string Notes, string Quantity)
        {
            if (key == "pmpkey")
            {
                Item item = new Item();

                item.deleteItem(mapToObject(Barcode, Type, Name, Description, Cost, Notes, Quantity));
            }
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public EfRecipe getRecipeById(string key, string recipeId)
        {
            if (key == "pmpkey")
            {
                Recipe item = new Recipe();

                return item.getRecipeById(recipeId);
            }
            return null;
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public EfRecipe getRecipeByName(string key, string name)
        {
            if (key == "pmpkey")
            {
                Recipe item = new Recipe();

                item.getRecipeByName(name);
            }
            return null;
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public List<EfRecipe> getRecipesByName(string key, string name)
        {
            if (key == "pmpkey")
            {
                Recipe item = new Recipe();

                item.getRecipesByName(name);
            }
            return null;
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public List<EfRecipe> getRecipesByDate(string key, DateTime date)
        {
            if (key == "pmpkey")
            {
                Recipe item = new Recipe();

                item.getRecipesByDate(date);
            }
            return null;
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public List<EfRecipe> getRecipesByDateRange(string key, DateTime start, DateTime end)
        {
            if (key == "pmpkey")
            {
                Recipe item = new Recipe();

                item.getRecipesByDateRange(start, end);
            }
            return null;
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public List<EfRecipe> getRecipesByItem(string key, string barcode)
        {
            if (key == "pmpkey")
            {
                Recipe item = new Recipe();

                item.getRecipesByItem(barcode);
            }
            return null;
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public List<EfRecipe> getRecipesByItemName(string key, string name)
        {
            if (key == "pmpkey")
            {
                Recipe item = new Recipe();

                item.getRecipesByItemName(name);
            }
            return null;
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public void addRecipe(string key, EfRecipe recipe)
        {
            if (key == "pmpkey")
            {
                Recipe item = new Recipe();

                item.addRecipe(recipe);
            }
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public void editRecipe(string key, EfRecipe recipe)
        {
            if (key == "pmpkey")
            {
                Recipe item = new Recipe();

                item.editRecipe(recipe);
            }
        }

        [WebMethod]
        [ScriptMethod(UseHttpGet = true)]
        public void deleteRecipe(string key, EfRecipe recipe)
        {
            if (key == "pmpkey")
            {
                Recipe item = new Recipe();

                item.deleteRecipe(recipe);
            } 
        }

        private EfItem mapToObject(string Barcode, string Type, string Name, string Description, string Cost, string Notes, string Quantity)
        {
            EfItem item = new EfItem();

            item.Barcode = long.Parse(Barcode);
            item.TypeID = int.Parse(Type);
            item.Name = Name;
            item.Description = Description;
            item.Cost = decimal.Parse(Cost);
            item.Notes = Notes;
            item.Quantity = int.Parse(Quantity);
            return item;
        }
    }
}
